<ul class="nav nav-tabs">
    <li class="active"><a href="#setup" data-toggle="tab">设置</a></li>
    <li><a href="#logs" data-toggle="tab">日志</a></li>
</ul>
<div id="scan-ip" class="tab-content">
    <div id="setup" class="tab-pane fade in active">
        <form method="POST" onSubmit="return false;">
            <div class="row-fluid">
                <div class="span2">
                    <label for="scan-ip-thread-num">扫描线程数</label>
                </div> <!-- .span4 -->
                <div class="span4">
                    <input id="scan-ip-thread-num" type="text" />
                </div> <!-- .span8 -->
                <div class="span2" >
                    <button class="btn btn-primary " id="update-thread-num">更新</button>
                </div>
            </div> <!-- .row-fluid -->
            <div class="row-fluid">
                <div class="span2">
                    <label for="ip-range-content">IP段列表(<a href="https://github.com/XX-net/XX-Net/wiki/GoAgent-Scan-Ip-Range-Format" target="_blank">支持格式</a>)</label>
                </div> <!-- .span2 -->
                <div class="span10">
                    <textarea id="ip-range-content" rows="10"></textarea>
                </div> <!-- .span10 -->
            </div> <!-- .row-fluid -->
            <div class="row-fluid">
                <div class="span12">
                    <button class="btn btn-primary btn-block" id="submit-ip-range" type="submit">提交</button>
                </div> <!-- .span12 -->
            </div> <!-- .row-fluid -->

        </form>
    </div> <!-- #authentication -->
    <div id="logs" class="tab-pane fade">
        <div id="log" class="span12"></div>
        <div class="row-fluid">
            <div class="span12">
                <button class="btn btn-primary btn-block" id="reload-scan-ip-log" type="submit">刷新</button>
            </div> <!-- .span12 -->
        </div> <!-- .row-fluid -->
    </div> <!-- #log -->
</div> <!-- #tab-content -->

<script type="text/javascript">
    title('扫描IP设置(一般不需要改动)');
</script>

<script type="text/javascript">
    function load_scan_ip_log(){
        $.ajax({
            type: 'GET',
            url: 'http://127.0.0.1:8084/scan_ip?cmd=get_scan_ip_log',
            dataType: 'html',
            success: function(result) {
                $('#log').html(result.replace(/\n/g, '<br>'));
            },
            error: function(){
                tip('读取日志失败.', 'error');
            }
        });
    }
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        var target = e.target.attributes.href.value;
        if(target == "#logs"){
            $('#log').scrollTop($('#log')[0].scrollHeight);
        }
    })
</script>
<script type="text/javascript">
    $(function() {
        $.ajax({
            type: 'POST',
            url: 'http://127.0.0.1:8084/config?cmd=get_config',
            dataType: 'JSON',
            success: function(result) {
                $('#scan-ip-thread-num').val(result['scan_ip_thread_num'] || 5);
            },
            error: function(){
                tip('读取设置失败.', 'error');
            }
        });
    });

    $('#ip-range-content').load('http://127.0.0.1:8084/scan_ip?cmd=get_range');

    load_scan_ip_log();
</script>
<script type="text/javascript">
    $('#reload-scan-ip-log').click(function(){
        load_scan_ip_log();
    });
</script>
<script type="text/javascript">
    $('#update-thread-num').click(function(){
        var scanIpThreadNum = $('#scan-ip-thread-num').val();
        var config = {
            'scan_ip_thread_num': scanIpThreadNum
        };

        $.ajax({
            type: 'POST',
            url: 'http://127.0.0.1:8084/scan_ip?cmd=set_scan_thread_num',
            data: config,
            dataType: 'JSON',
            success: function(result) {
                if ( result['res'] == 'success' ) {
                    tip('设置已成功保存.', 'success');
                } else {
                    tip('发生未知错误.', 'error');
                }
            }
        });
    });
</script>
<script type="text/javascript">
    $('#submit-ip-range').click(function(){
        var ipRangeContent = $('#ip-range-content').val();
        var config = {
            'ip_range': ipRangeContent
        };

        $.ajax({
            type: 'POST',
            url: 'http://127.0.0.1:8084/scan_ip?cmd=set_range',
            data: config,
            dataType: 'JSON',
            success: function(result) {
                if ( result['res'] == 'success' ) {
                    tip('设置已成功保存.', 'success');
                } else {
                    tip('发生未知错误.', 'error');
                }
            }
        });
    });
</script>